/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.siga.modelo.DAO;

import com.siga.modelo.entidad.Aula;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.jasper.tagplugins.jstl.core.ForEach;

/**
 *
 * @author karolyn
 */
public class AulaDAO {
    private EntityManager em;
    private EntityManagerFactory emf;
    
    public AulaDAO(){
        emf=Persistence.createEntityManagerFactory("AppSigaPPU");
        em=emf.createEntityManager();
    }
    
    public void closeEntityManager(){
        em.close();
    }
    
    public boolean insertarAula(Aula a){
        boolean flag=false;
        try{
            em.getTransaction().begin();
            em.persist(a);
            em.getTransaction().commit();
            flag=true;
        } catch (Exception e){em.getTransaction().rollback();}
        return flag;
    }
    
    public Aula buscarAula(String nombre, int tipo){
        if(tipo==1)
            return em.find(Aula.class, Integer.parseInt(nombre));
        else
            return (Aula) em.createNamedQuery("Aula.findByDescAula").setParameter("descAula", nombre).getSingleResult();
            
    }
    
    public boolean eliminarAula(int codAula){
        boolean flag=false;
        try{
            em.getTransaction().begin();
            em.remove(em.find(Aula.class, codAula));
            em.getTransaction().commit();
            flag=true;
        } catch (Exception e){em.getTransaction().rollback();}
        return flag;
    }
    
    public List<Aula> listarAulas(){
        Query q = em.createQuery("SELECT a FROM Aula a");
        return q.getResultList();
    }
    
}
